﻿define([
    'Wjs/ui/elements/util/Debouncer',
    'Wjs/ui/elements/notifiers/Notifier'
],
function () {
    'use strict';

    var Debouncer = Wjs.getType('Debouncer'),
        Notifier = Wjs.getType('Notifier');

    return Wjs.defineOnce({

        type: 'resizeNotifier',

        'static': true,

        base: Notifier,

        initialize: function ()
        {
            var me = this;

            me.debouncer = new Debouncer();

            me.debouncer.callback = function () {

                me.notify();
            }
        },

        // For static types this is fired when the instance is created
        onTypeDefined: function (resizeNotifier) {

            window.onresize = function () {

                resizeNotifier.onResize();
            };

        },

        // Notifies the subscribers about the resize event
        onResize: function () {

            this.debouncer.nextTick();
        },

        notify: function () {

            this.views.forEach(function (view) {

                if (view.onResize) {

                    view.onResize();
                }
            });
        }

    });

});